package com.cup.bddm.api.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.api.entity.UserLogEntity;
import com.cup.bddm.api.mapper.UserLogEntityMapper;
import com.cup.bddm.api.req.UserVo;

@Service
public class UserLogService {
	@Autowired
	UserLogEntityMapper userLogEntityMapper;

	static ExecutorService pool = Executors.newFixedThreadPool(10);

	public void asyncSave( //
			final String cup_branch_ins_id_cd, //
			final String login_name, //
			final String req_ip, //
			final long req_time, //
			final String req_type, //
			final String res_stat, //
			final String req_src) {
		pool.execute(new Runnable() {
			@Override
			public void run() {
				UserLogEntity entity = new UserLogEntity();
				entity.setCup_branch_ins_id_cd(cup_branch_ins_id_cd);
				entity.setLogin_name(login_name);
				entity.setReq_ip(req_ip);
				entity.setReq_time(new Date(req_time));
				entity.setReq_type(req_type);
				entity.setRes_stat(res_stat);
				entity.setReq_src(req_src);
				userLogEntityMapper.save(entity);
			}
		});
	}

	public List<UserLogEntity> findByVo(UserVo vo) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("login_name", vo.getLogin_name());
		return userLogEntityMapper.pageByPms(pms, vo.pager());
	}

	// 最后一次登录成功时间
	public String lastLoginTime(UserVo vo) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("login_name", vo.getLogin_name());
		UserLogEntity entity = userLogEntityMapper.lastLogin(pms);
		return entity == null || entity.getReq_time() == null ? "" : new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(entity.getReq_time());
		
	}
}
